package double_pointer;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86185
 * Date: 2023-12-03
 * Time: 11:20
 */
public class DeleteDuplicates {
    public static ListNode deleteDuplicates(ListNode head) {
        if (head == null || head.next == null) {
            return head;
        }
        ListNode fast = head;
        ListNode newHead = new ListNode(0, head);
        ListNode slow = newHead;
        while (fast != null && fast.next != null) {
            if(fast.next.val == slow.next.val) {
                while (fast.next != null && fast.next.val == slow.next.val) {
                    fast = fast.next;
                }
                slow.next = fast.next;
            } else {
                slow = slow.next;
            }
            fast = fast.next;
        }
        return newHead.next;
    }
}
